#include <stdio.h>
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "../display/dispcolor.h"
#include "messagebox.h"


//==============================================================================
// 该过程在屏幕中央显示一条消息
//==============================================================================
void message_show(uint16_t width, uint8_t ItemFont, char *pTitle, char *pMessage, uint16_t lineColor, uint8_t darkBg, uint16_t delay_ms)
{
	// 计算所需的窗口宽度
	uint16_t titleWidth = dispcolor_getStrWidth(ItemFont, pTitle);
	uint16_t messageWidth = dispcolor_getStrWidth(ItemFont, pMessage);
	uint16_t windowWidth = width ? width : titleWidth > messageWidth ? titleWidth + 20 : messageWidth + 10;
	uint16_t windowHeight = 40;

	// 我们计算窗口的坐标
	uint16_t dispWidth = dispcolor_getWidth();
	uint16_t startX = (dispWidth - windowWidth) / 2;
	uint16_t startY = 100;
	uint16_t endX = dispWidth - startX;
	uint16_t endY = startY + windowHeight;

	// 使背景变暗
	if (darkBg)
		dispcolor_screenDark();
	// 用黑色填充窗口
	dispcolor_DrawRectangleFilled(startX, startY, endX, endY, BLACK);

	// 显示窗口元素
	uint16_t yPos = 0;
	// 标题
	dispcolor_DrawRectangleFilled(startX + 2, startY + yPos + 2, endX - 2, startY + yPos + 12, WHITE);
	dispcolor_printf(startX + (windowWidth - titleWidth) / 2, startY + yPos + 4, FONTID_6X8M, BLACK, pTitle);
	yPos += 16;
	// 消息文本
	dispcolor_printf(startX + 5, startY + yPos + 4, FONTID_6X8M, WHITE, pMessage);
	yPos += 16;
	// 底部有彩色条纹
	dispcolor_DrawRectangleFilled(startX + 2, startY + yPos + 2, endX - 2, startY + yPos + 5, lineColor);

	dispcolor_Update();
	vTaskDelay(delay_ms / portTICK_RATE_MS);
}
//==============================================================================


//==============================================================================
// 该过程显示一个带有消息和颜色进度条的窗口。
//==============================================================================
void progress_show(uint16_t width, uint8_t ItemFont, char *pTitle, char *pMessage, uint16_t progressColor, uint16_t progressValue, uint16_t progressMax)
{
	// 计算所需的窗口宽度
	uint16_t titleWidth = dispcolor_getStrWidth(ItemFont, pTitle);
	uint16_t messageWidth = dispcolor_getStrWidth(ItemFont, pMessage);
	uint16_t windowWidth = width ? width : titleWidth > messageWidth ? titleWidth + 20 : messageWidth + 10;
	uint16_t windowHeight = 40;

	// 我们计算窗口的坐标
	uint16_t dispWidth = dispcolor_getWidth();
	uint16_t startX = (dispWidth - windowWidth) / 2;
	uint16_t startY = 100;
	uint16_t endX = dispWidth - startX;
	uint16_t endY = startY + windowHeight;

	// 用黑色填充窗口
	dispcolor_DrawRectangleFilled(startX, startY, endX, endY, BLACK);

	// 显示窗口元素
	uint16_t yPos = 0;
	// 标题
	dispcolor_DrawRectangleFilled(startX + 2, startY + yPos + 2, endX - 2, startY + yPos + 12, WHITE);
	dispcolor_printf(startX + (windowWidth - titleWidth) / 2, startY + yPos + 4, FONTID_6X8M, BLACK, pTitle);
	yPos += 16;
	// 消息文本
	dispcolor_printf(startX + 5, startY + yPos + 4, FONTID_6X8M, WHITE, pMessage);
	yPos += 16;
	// 底部有彩色条纹
	if (progressMax)
	{
		uint32_t endLineX = windowWidth - 4;
		endLineX = endLineX * progressValue / progressMax;
		dispcolor_DrawRectangleFilled(startX + 2, startY + yPos + 2, startX + endLineX, startY + yPos + 5, progressColor);
	}

	dispcolor_Update();
}
//==============================================================================

//==============================================================================
// 该过程显示一个消息框
//==============================================================================
void progress_start_show(uint16_t width, uint8_t ItemFont, char *pTitle, char *pMessage, uint16_t progressColor, uint16_t progressValue, uint16_t progressMax)
{
	// 使背景变暗
	dispcolor_screenDark();
	progress_show(width, ItemFont, pTitle, pMessage, progressColor, progressValue, progressMax);
}
//==============================================================================

